var path = require("path");
var memberDAO = require(path.join(process.cwd(),"dao/MemberDAO"));
var Password = require("node-php-password");
var logger = require('../modules/logger').logger();


module.exports.addMember = function(params,cb) {

    memberDAO.exists(params.id,function(err,isExists){
        if(err) return cb(err);

        if(isExists) {
            return cb("学号已存在");
        }

        memberDAO.create({
            "user_id" :params.user_id,
            "user_name" : params.user_name,
            "sex" : params.sex,
            "class" : params.class,
            "type" : params.type,
            "lib":params.lib,
            "teacher":params.teacher,
            "tel":params.tel,
            "addr":params.addr,
            "qq":params.qq,
            "status":params.status,
            "remark":params.remark
        },function(err,member){
            if(err) {
                console.log(err);
                return cb("创建失败");}
            result = {
                "user_id" :member.user_id,
                "user_name" : member.user_id,
                "sex" : member.sex,
                "class" : member.class,
                "type" : member.type,
                "library":member.lib,
                "teacher":member.teacher,
                "tel":member.tel,
                "addr":member.addr,
                "qq":member.qq,
                "status":member.status,
                "remark":member.remark
            };
            cb(null,result);
        });
    });
}


module.exports.updateMember = function(id,params,cb) {
    memberDAO.update(id,params,function(err,member){
        if(err) {
            console.log(err);
            return cb("更新失败");}
        result = "更新失败";
        cb(null,result);
    });
}



module.exports.getAllMembers = function(conditions,cb) {

    if(!conditions.pagenum) return cb("pagenum 参数不合法");
    if(!conditions.pagesize) return cb("pagesize 参数不合法");
    // 通过关键词获取管理员数量
    memberDAO.countByKey(conditions["query"],function(err,count) {
        key = conditions["query"];
        pagenum = parseInt(conditions["pagenum"]);
        pagesize = parseInt(conditions["pagesize"]);
        type=conditions["type"]

        pageCount = Math.ceil(count / pagesize);
        offset = (pagenum - 1) * pagesize;
        if(offset >= count) {
            offset = count;
        }
        limit = pagesize;

        memberDAO.findByKey(key,offset,limit,type,function(err,members){
            var retMembers = [];
            for(idx in members) {
                var member = members[idx];

                retMembers.push({
                    "i":member.id,
                    "user_id": member.user_id,
                    "user_name":member.user_name,
                    "sex":member.sex,
                    "class":member.class,
                    "type":member.type,
                    "lib":member.lib,
                    "teacher":member.teacher,
                    "tel":member.tel,
                    "addr":member.addr,
                    "qq":member.qq,
                    "remark":member.remark,
                    "status":member.status == 1

                });
            }
            var resultDta = {};
            resultDta["total"] = count;
            resultDta["pagenum"] = pagenum;
            resultDta["members"] = retMembers;
            cb(err,resultDta);
        });
    });
}

module.exports.getMemberById = function(id,cb) {
    memberDAO.findByID(id,cb)
}

module.exports.deleteMember = function(user_id,cb){
    memberDAO.deleteByUserID(user_id,cb)
}
